package com.ledim.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.activity.Card9ListActivity;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimCardGroupBean;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimViewHolder;
import com.letv.android.young.client.R;
import com.letv.tracker2.enums.EventType;
import com.letv.tracker2.enums.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 精选页adapter
 * ccx
 */
public class ChoiceCardAdapter extends LedimBaseListAdapter<LedimCardGroupBean> {

    Activity mActivity;
    public Map<String, String> mCardIds;

    public ChoiceCardAdapter(Activity c) {
        super(c);
        mActivity = c;
        mCardIds = new HashMap<String, String>();
    }

    @Override
    public int getViewTypeCount() {
        return 10;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).style;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LedimCardGroupBean cardGroup = getItem(position);
        int viewType = getItemViewType(position);
        LedimViewHolder viewHolder = null;
        switch (viewType) {
            case ConstantUtil.CardGroupStyle.card2:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a2_list);
                break;
            case ConstantUtil.CardGroupStyle.card3:
            case ConstantUtil.CardGroupStyle.card5:
            case ConstantUtil.CardGroupStyle.card7:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a3_list);
                break;
            case ConstantUtil.CardGroupStyle.card6:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a6_list);
                break;

            case ConstantUtil.CardGroupStyle.card4:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a4_list);
                break;
            case ConstantUtil.CardGroupStyle.card8:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a8_list);
                break;
            case ConstantUtil.CardGroupStyle.card9:
                viewHolder = LedimViewHolder.get(context, convertView, R.layout.card_a9_list);
                break;

        }
        mCardIds.put(cardGroup.id, "1");
        if (viewHolder != null) {
            if (viewType != ConstantUtil.CardGroupStyle.card9) {
                getAdapter(cardGroup).initView(viewHolder);
            } else {
                FrescoUtil.loadUrl(cardGroup.photo, (SimpleDraweeView) viewHolder.getView(R.id.card_a9_item_photo));
                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转专题列表页
                        Bundle bundle = new Bundle();
                        bundle.putString(ConstantUtil.TITLE, cardGroup.title);
                        bundle.putString(ConstantUtil.DES, cardGroup.intro);
                        bundle.putString(ConstantUtil.ID, cardGroup.id);
                        bundle.putString(ConstantUtil.PHOTO, cardGroup.photo);
                        ((LeDimBaseActivity) mActivity).goToOthers(Card9ListActivity.class, bundle);
                    }
                });
            }

            return viewHolder.getConvertView();
        }
        SimpleDraweeView simpleDraweeView = new SimpleDraweeView(getContext());
        simpleDraweeView.setImageResource(R.drawable.default_image);
        convertView = simpleDraweeView;
        return convertView;
    }

    public void postCardExposeEvent() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!mCardIds.isEmpty()) {
                    Set<Map.Entry<String, String>> sets = mCardIds.entrySet();
                    for (Map.Entry<String, String> entry : sets) {
                        HashMap<String, String> params = new HashMap<String, String>();
                        params.put(Key.Type.name(), "homepageCard");
                        params.put("fragId", entry.getKey());
                        LeDimAnalysisUtils.reportEvent(LeDimAnalysisUtils.CHOICE_PAGE_CODE, EventType.Expose, params);
                    }
                    mCardIds.clear();
                }
            }
        }, 500);

    }

    private CarGroupBaseAdapter getAdapter(LedimCardGroupBean cardGroupBean) {
        switch (cardGroupBean.style) {
            case ConstantUtil.CardGroupStyle.card4:
                return new CardGroup4Adapter(mActivity, cardGroupBean);
            case ConstantUtil.CardGroupStyle.card8:
                return new CardGroup8Adapter(mActivity, cardGroupBean);
            default:
                return new CardGroupListAdapter(mActivity, cardGroupBean);
        }
    }

}
